package com.dhwl.hsh.merchant.presenter;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.dhwl.hsh.lib.bean.HttpResponse;
import com.dhwl.hsh.lib.bean.req.AddTaskReq;
import com.dhwl.hsh.lib.bean.resp.TaskResp;
import com.dhwl.hsh.lib.core.HshException;
import com.dhwl.hsh.lib.core.mvp.BasePresenter;
import com.dhwl.hsh.lib.utils.ReservoirUtils;
import com.dhwl.hsh.lib.utils.ToastUtils;
import com.dhwl.hsh.lib.utils.logger.Logger;
import com.dhwl.hsh.merchant.HshApplication;
import com.dhwl.hsh.merchant.R;
import com.dhwl.hsh.merchant.model.impl.TaskModel;
import com.dhwl.hsh.merchant.presenter.iview.IAddTaskView;
import com.dhwl.hsh.merchant.ui.activity.BaseActivity;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.schedulers.Schedulers;

/**
 * 简要描述
 *
 * @author Derek.Kidd
 * @version V1.0.0
 * @date 2016-04-18 18:48
 */
public class AddTaskPresenter extends BasePresenter<IAddTaskView> {
    private ReservoirUtils reservoirUtils;

    public AddTaskPresenter() {
        this.reservoirUtils = new ReservoirUtils();
    }

    public void costMoney(AddTaskReq addTaskReq) {
        Subscriber<HttpResponse<String>> subscriber = new Subscriber<HttpResponse<String>>() {

            @Override
            public void onCompleted() {
                if (AddTaskPresenter.this.mCompositeSubscription != null)
                    AddTaskPresenter.this.mCompositeSubscription.remove(this);
            }

            @Override
            public void onError(Throwable e) {
                Logger.e("costMoney", e.getMessage());
                ((BaseActivity) AddTaskPresenter.this.getMvpView()).hideWaitingDialog();
                ToastUtils.show(HshApplication.mContext, R.string.network_not_exist, Toast.LENGTH_SHORT);
            }

            @Override
            public void onNext(HttpResponse<String> response) {
                if (response.getStatus() != 0) {
                    HshException exception = new HshException(response.getStatus(), response.getDescribe());
                    AddTaskPresenter.this.getMvpView().onCostMoneyFailure(exception);
                } else {
                    Logger.d("costMoney-result:" + response.getData());
                    AddTaskPresenter.this.getMvpView().onCostMoneySuccess(Double.valueOf(response.getData()));
                }
            }
        };
        this.mCompositeSubscription.add(TaskModel.getInstance().costMoney(addTaskReq).subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).doOnSubscribe(new Action0() {
            @Override
            public void call() {
                ((BaseActivity) AddTaskPresenter.this.getMvpView()).showWaitingDialog();
            }
        }).doAfterTerminate(new Action0() {
            @Override
            public void call() {
                ((BaseActivity) AddTaskPresenter.this.getMvpView()).hideWaitingDialog();
            }
        }).subscribe(subscriber));
    }

    public void addTask(AddTaskReq addTaskReq) {

        Logger.d(addTaskReq.toString());

        Subscriber<HttpResponse<TaskResp>> subscriber = new Subscriber<HttpResponse<TaskResp>>() {

            @Override
            public void onCompleted() {
                if (AddTaskPresenter.this.mCompositeSubscription != null)
                    AddTaskPresenter.this.mCompositeSubscription.remove(this);
            }

            @Override
            public void onError(Throwable e) {
                Log.e("addTask", e.getMessage());
                ((BaseActivity) AddTaskPresenter.this.getMvpView()).hideWaitingDialog();
                ToastUtils.show(HshApplication.mContext, R.string.network_not_exist, Toast.LENGTH_SHORT);
            }

            @Override
            public void onNext(HttpResponse<TaskResp> response) {

                if (response.getStatus() != 0) {
                    ToastUtils.show((Context) getMvpView(), response.getDescribe(), Toast.LENGTH_LONG);
                    HshException exception = new HshException(response.getStatus(), response.getDescribe());
                    AddTaskPresenter.this.getMvpView().onAddTaskFailure(exception);
                } else {
                    AddTaskPresenter.this.getMvpView().onAddTaskSuccess(null);
                }
            }
        };
        this.mCompositeSubscription.add(TaskModel.getInstance().addTask(addTaskReq).subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).doOnSubscribe(new Action0() {
            @Override
            public void call() {
                ((BaseActivity) AddTaskPresenter.this.getMvpView()).showWaitingDialog();
            }
        }).doAfterTerminate(new Action0() {
            @Override
            public void call() {
                ((BaseActivity) AddTaskPresenter.this.getMvpView()).hideWaitingDialog();
            }
        }).subscribe(subscriber));

    }


}
